package com.xm.xmapp.tools;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.xm.xmapp.LongReRentOrderActivity;
import com.xm.xmapp.LongRentOrderDetailsActivity;
import com.xm.xmapp.LongRentOrderPayActivity;
import com.xm.xmapp.MainActivity;
import com.xm.xmapp.MyWalletActivity;
import com.xm.xmapp.PayActivity;
import com.xm.xmapp.PileOrderDetailActivity;
import com.xm.xmapp.PileOrderPayActivity;
import com.xm.xmapp.ShortTimeOrderPayActivity;
import com.xm.xmapp.WZYJActivity;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.customview.LoadingDialog;
import com.xm.xmapp.fragment.LongRentOrderFragment;
import com.xm.xmapp.fragment.PileOrderFragment;
import com.xm.xmapp.http.ThreadPoolFactory;
import com.xm.xmapp.myinterface.ZFBInterFace;
import com.xm.xmapp.response.bean.ResponseWXpaysign;
import com.xm.xmapp.wxapi.WXPayEntryActivity;

public class PayUitl {

	private static PayUitl instence;
	private ZFBInterFace zfbInterFace;

	private PayUitl() {
	}

	public static PayUitl getInstence() {
		if (instence == null) {
			instence = new PayUitl();
		}
		return instence;
	}

	/**
	 * 调起微信支付
	 * 
	 * @param cotext
	 * @param wxBean
	 *            服务器接口请求的支付参数
	 * @param promptDialog
	 *            弹窗处理
	 */
	public void genWxPay(Context cotext, ResponseWXpaysign wxBean, LoadingDialog promptDialog) {
		final IWXAPI wxApi = WXAPIFactory.createWXAPI(cotext, null);
		wxApi.registerApp(wxBean.appid);// wx6170f5118eeac379
		PayReq req = new PayReq();
		req.appId = wxBean.appid;
		CodeConstant.APPID = wxBean.appid;
		req.partnerId = wxBean.partnerid;
		req.prepayId = wxBean.prepayid;
		req.packageValue = wxBean.package_str;
		req.nonceStr = wxBean.noncestr;
		req.timeStamp = wxBean.timestamp;
		req.sign = wxBean.sign;

		if (isWXAppInstalledAndSupported(cotext)) {
			if (promptDialog != null) {
				promptDialog.show();
			}
			wxApi.sendReq(req);

		} else {
			Tools.showMessage("未安装微信");
		}
	}

	/**
	 * 测试
	 * 
	 * @param wxApi
	 * @param wxBean
	 */
	public void testWcPay(IWXAPI wxApi, ResponseWXpaysign wxBean) {
		PayReq req = new PayReq();
		req.appId = wxBean.appid;
		CodeConstant.APPID = wxBean.appid;
		req.partnerId = wxBean.partnerid;
		req.prepayId = wxBean.prepayid;
		req.packageValue = wxBean.package_str;
		req.nonceStr = wxBean.noncestr;
		req.timeStamp = wxBean.timestamp;
		req.sign = wxBean.sign;
		wxApi.sendReq(req);
	}

	private boolean isWXAppInstalledAndSupported(Context cotext) {
		IWXAPI msgApi = WXAPIFactory.createWXAPI(cotext, null);
		msgApi.registerApp(CodeConstant.APPID);
		boolean sIsWXAppInstalledAndSupported = msgApi.isWXAppInstalled() && msgApi.isWXAppSupportAPI();
		return sIsWXAppInstalledAndSupported;
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			{
				PayResult payResult = new PayResult((String) msg.obj);
				// 支付宝返回此次支付结果及加签，建议对支付宝签名信息拿签约时支付宝提供的公钥做验签
				// String resultInfo = payResult.getResult();
				String resultStatus = payResult.getResultStatus();
				// 判断resultStatus 为“9000”则代表支付成功，具体状态码代表含义可参考接口文档
				if (TextUtils.equals(resultStatus, "9000")) {
					// Toast.makeText(MyAppcation.getMyAppcation(), "订单支付成功",
					// Toast.LENGTH_SHORT).show();
					instence.zfbInterFace.success();
				} else {
					if (TextUtils.equals(resultStatus, "4000")) {
						Toast.makeText(MyAppcation.getMyAppcation(), "订单支付失败", Toast.LENGTH_SHORT).show();
					} else if (TextUtils.equals(resultStatus, "8000")) {
						Toast.makeText(MyAppcation.getMyAppcation(), "正在处理中", Toast.LENGTH_SHORT).show();
					} else if (TextUtils.equals(resultStatus, "6001")) {
						Toast.makeText(MyAppcation.getMyAppcation(), "支付已取消", Toast.LENGTH_SHORT).show();
					} else if (TextUtils.equals(resultStatus, "6002")) {
						Toast.makeText(MyAppcation.getMyAppcation(), "网络连接出错", Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(MyAppcation.getMyAppcation(), "支付失败", Toast.LENGTH_SHORT).show();
					}
				}

			}
		}
	};

	public void zfb(final Activity activity, ZFBInterFace zfbInterFace, final String sign) {

		instence.zfbInterFace = zfbInterFace;
		Runnable payRunnable = new Runnable() {

			@Override
			public void run() {
				// 构造PayTask 对象
				PayTask alipay = new PayTask(activity);
				// 调用支付接口，获取支付结果
				String result = alipay.pay(sign, true);
				Message msg = new Message();
				msg.obj = result;
				mHandler.sendMessage(msg);
			}
		};
		ThreadPoolFactory.getInstance().execute(payRunnable);

	}

	/**
	 * 支付完成后调用
	 */
	public void afterPayMoney() {

		Tools.showMessage("支付成功");
		switch (WXPayEntryActivity.currentStatus) {
		case WXPayEntryActivity.LONGYUYUE:
			MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.LONGRENT_ORDER_CHANGE);
			MyAppcation.getMyAppcation().notifyDataSetChanged(LongRentOrderPayActivity.class,
					Type.A_LongRentOrderPay.DEFAULT);

			break;
		case WXPayEntryActivity.LONGRERENT:
			MyAppcation.getMyAppcation().notifyDataSetChanged(LongRentOrderPayActivity.class,
					Type.A_LongRentOrderPay.DEFAULT);
			MyAppcation.getMyAppcation().notifyDataSetChanged(LongRentOrderDetailsActivity.class,
					Type.A_LongRentOrderDetails.PAY_SUCCESS);
			MyAppcation.getMyAppcation().notifyDataSetChanged(LongReRentOrderActivity.class,
					Type.A_LongReRentOrder.DEFAULT);
			MyAppcation.getMyAppcation()
					.notifyDataSetChanged(LongRentOrderFragment.class, Type.F_LongRentOrder.DEFAULT);
			MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.LONGRENT_ORDER_CHANGE);
			break;

		case WXPayEntryActivity.FENSHIHC:
			MyAppcation.getMyAppcation().notifyDataSetChanged(ShortTimeOrderPayActivity.class, Type.A_ShortTimeOrderPay.DEFAULT);
			MyAppcation.getMyAppcation().notifyDataSetChanged(PayActivity.class, Type.A_Pay.DEFAULT);

			break;
		case WXPayEntryActivity.WALLET:
			MyAppcation.getMyAppcation().notifyDataSetChanged(MyWalletActivity.class, Type.A_MyWallet.GET_WALLET);
			MyAppcation.getMyAppcation().notifyDataSetChanged(PayActivity.class, Type.A_Pay.DEFAULT);
			MyAppcation.getMyAppcation().notifyDataSetChanged(LongReRentOrderActivity.class,
					Type.A_LongReRentOrder.PAY_CAR);
			break;

		case WXPayEntryActivity.WZ:
			MyAppcation.getMyAppcation().notifyDataSetChanged(MyWalletActivity.class, Type.A_MyWallet.GET_WALLET);
			MyAppcation.getMyAppcation().notifyDataSetChanged(WZYJActivity.class, Type.A_WZYJ.DEFAULT);
			break;
		case WXPayEntryActivity.PILE:
			MyAppcation.getMyAppcation().notifyDataSetChanged(PileOrderDetailActivity.class, Type.A_PileOrderDetail.DEFAULT);
			MyAppcation.getMyAppcation().notifyDataSetChanged(PileOrderPayActivity.class,Type.A_PileOrderPay.DEFAULT);
			MyAppcation.getMyAppcation().notifyDataSetChanged(PileOrderFragment.class, Type.F_PileOrder.DEFAULT);

			break;

		default:
			break;
		}
	}

}
